package org.linlinjava.litemall.db.service;

import java.time.LocalDateTime;
import java.util.List;

import javax.annotation.Resource;

import org.linlinjava.litemall.db.BeetlParams;
import org.linlinjava.litemall.db.domain.LitemallGoodsProduct;
import org.linlinjava.litemall.db.mapper.LitemallGoodsProductDao;
import org.springframework.stereotype.Service;

@Service
public class LitemallGoodsProductService {
    @Resource
    private LitemallGoodsProductDao litemallGoodsProductDao;

    public List<LitemallGoodsProduct> queryByGid(Integer gid) {

    	LitemallGoodsProduct  goodsProduct = new LitemallGoodsProduct();
    	goodsProduct.setGoodsId(gid);
    	goodsProduct.setDeleted(false);
    	return litemallGoodsProductDao.template(goodsProduct);
		/*
		 * LitemallGoodsProductExample example = new LitemallGoodsProductExample();
		 * example.or().andGoodsIdEqualTo(gid).andDeletedEqualTo(false); return
		 * litemallGoodsProductMapper.selectByExample(example);
		 */
    }

    public LitemallGoodsProduct findById(Integer id) {
    	return litemallGoodsProductDao.single(id);
       // litemallGoodsProductMapper.selectByPrimaryKey(id);
    }

    public void deleteById(Integer id) {
    	LitemallGoodsProduct goodsProduct  = new LitemallGoodsProduct();
    	goodsProduct.setId(id);
    	goodsProduct.setDeleted(true);
    	litemallGoodsProductDao.updateTemplateById(goodsProduct);
        //litemallGoodsProductMapper.logicalDeleteByPrimaryKey(id);
    }

    public void add(LitemallGoodsProduct goodsProduct) {
        goodsProduct.setAddTime(LocalDateTime.now());
        goodsProduct.setUpdateTime(LocalDateTime.now());
        litemallGoodsProductDao.insertTemplate(goodsProduct,true);
        //litemallGoodsProductMapper.insertSelective(goodsProduct);
    }

    public int count() {
    	
    	LitemallGoodsProduct goodsProduct  = new LitemallGoodsProduct();
    	goodsProduct.setDeleted(false);
    	return (int)litemallGoodsProductDao.templateCount(goodsProduct);
		/*
		 * LitemallGoodsProductExample example = new LitemallGoodsProductExample();
		 * example.or().andDeletedEqualTo(false); return (int)
		 * litemallGoodsProductMapper.countByExample(example);
		 */
    }

    public void deleteByGid(Integer gid) {
    	LitemallGoodsProduct goodsProduct  = new LitemallGoodsProduct();
    	goodsProduct.setDeleted(true);
    	goodsProduct.setGoodsId(gid);
    	litemallGoodsProductDao.updateTemplateById(goodsProduct);
		/*
		 * LitemallGoodsProductExample example = new LitemallGoodsProductExample();
		 * example.or().andGoodsIdEqualTo(gid);
		 * litemallGoodsProductMapper.logicalDeleteByExample(example);
		 */
    }

    public int addStock(Integer id, Short num){
    	return litemallGoodsProductDao.executeUpdate("update litemall_goods_product\n" + 
    			" set number = number + #num#, update_time = #updatetime#\n" + 
    			" where id = #id#", 
    			BeetlParams.create()
    			.set("number", num)
    			.set("updatetime", LocalDateTime.now())
    			.set("id", id));
    	
		/*
		 * LitemallGoodsProduct goodsProduct = new LitemallGoodsProduct();
		 * 
		 * return goodsProductDao.addStock(id, num);
		 */
    }

    public int reduceStock(Integer id, Short num){
    	return litemallGoodsProductDao.executeUpdate("update litemall_goods_product\n" + 
    			" set number = number - #num#, update_time = #updatetime#\n" + 
    			" where id = #id# and number>=#num#", 
    			BeetlParams.create()
    			.set("number", num)
    			.set("updatetime", LocalDateTime.now())
    			.set("id", id));
    	
        //return goodsProductDao.reduceStock(id, num);
    }

    public void updateById(LitemallGoodsProduct product) {
        product.setUpdateTime(LocalDateTime.now());
        litemallGoodsProductDao.updateTemplateById(product);
       // litemallGoodsProductMapper.updateByPrimaryKeySelective(product);
    }
}